package logic;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: nocturne
 * Date: 03.03.13
 * Time: 11:37
 * To change this template use File | Settings | File Templates.
 */
public class SwingGui extends JFrame {
    private JPanel panel2;
    private JPanel mainPanel;
    private JButton btnOnLED1;
    private JLabel labelLED1;
    private JButton btnOnLED2;
    private JLabel labelLED2;
    private JList listMain;

    private ArrayList<String> listData = new ArrayList<String>();


    private boolean isPushBtn1 = false;
    private boolean isPushBtn2 = false;

    public SwingGui() {
        createUIComponents();
    }

    private void createUIComponents() {

/*        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("CDE/Motif".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
            System.out.println("look and feel error");
        }*/

        setContentPane(mainPanel);
        setTitle("Control Arduino");
        setSize(600, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //center
        setLocationRelativeTo(null);

        setVisible(true);


        btnOnLED1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                new Thread() {
                    @Override
                    public void run() {
                        if (!isPushBtn1) {
                            doCommand("onLED1");
                            isPushBtn1 = true;

                            labelLED1.setText("LED1 is On");
                        } else {
                            doCommand("offLED1");
                            isPushBtn1 = false;

                            btnOnLED1.setText("LED1 is Off");
                        }
                    }
                }.start();
            }
        });


        btnOnLED2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread() {
                    @Override
                    public void run() {
                        if (!isPushBtn2) {
                            doCommand("onLED2");
                            isPushBtn2 = true;

                            labelLED2.setText("LED2 is On");
                        } else {
                            doCommand("offLED2");
                            isPushBtn2 = false;

                            btnOnLED2.setText("LED2 is Off");
                        }
                    }
                }.start();
            }
        });

    }

    public ArrayList<String> getListData() {
        return listData;
    }

    public void setListData(ArrayList<String> listData) {
        this.listData = listData;
    }


    private void doCommand(String command) {

        addSockets(command, "1");
    }

    private String addSockets(String command, String param) {

        String result = null;
        String[] json = {command, param};

        try {
            //Socket s = new Socket("192.168.1.101", 1234);
            Socket s = new Socket("192.168.1.106", 1234);

            ObjectOutputStream out;
            ObjectInputStream in;

            out = new ObjectOutputStream(s.getOutputStream());
            in = new ObjectInputStream(s.getInputStream());

            out.writeObject(json);
            out.flush();

            // response server
            try {
                result = (String) in.readObject();
                System.out.println(result);
            } catch (ClassNotFoundException e) {
                System.out.println("RESPONSE SERVER ERROR: " + e);
            }


            in.close();
            out.close();

            return result;


        } catch (IOException e) {
            System.out.println(e);
        }

        return null;
    }


}
